/* Copyright (c) 2006 by M Aamir Maniar 
 * 
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to 
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be 
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * */

namespace Generic.DataStructures
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Generic.Utils;

	public class SynchronizedStack<T> : IStack<T>
	{
		object _syncRoot;
		public SynchronizedStack(IStack<T> stack)
		{
			ArgumentValidator.CheckIfNull<IStack<T>>(stack);
			_innerStack = stack;
			_syncRoot = stack.SyncRoot;
		}

		#region StackSynchronized
		private IStack<T> _innerStack;
		/// <summary>
		/// Gets or sets the inner stack.
		/// </summary>
		/// <value>The inner stack.</value>
		public IStack<T> InnerStack
		{
			get { return _innerStack; }
			set { _innerStack = value; }
		}
		#endregion

		#region IStack<T> Members

		public void Push(T item)
		{
			lock (_syncRoot)
			{
				_innerStack.Push(item);
			}
		}

		public T Pop()
		{
			lock (_syncRoot)
			{
				return _innerStack.Pop();
			}
		}

		public T Top
		{
			get 
			{ 
				lock (_syncRoot)
				{
					return _innerStack.Top;
				}
			}
		}

		public bool FixedSize
		{
			get
			{
				lock (_syncRoot)
				{
					return _innerStack.FixedSize;
				}
			}
			set
			{
				lock (_syncRoot)
				{
					_innerStack.FixedSize = value;
				}
			}
		}

		public bool IsFull
		{
			get 
			{
				lock (_syncRoot)
				{
					return _innerStack.IsFull;
				}
			}
		}

		public bool IsEmpty
		{
			get 
			{
				lock (_syncRoot)
				{
					return _innerStack.IsEmpty;
				}
			}
		}

		public int Capacity
		{
			get
			{
				lock (_syncRoot)
				{
					return _innerStack.Capacity;
				}
			}
			set
			{
				lock (_syncRoot)
				{
					_innerStack.Capacity = value;
				}
			}
		}

		public StackGrowingMode GrowMode
		{
			get
			{
				lock (_syncRoot)
				{
					return _innerStack.GrowMode;
				}
			}
			set
			{
				lock (_syncRoot)
				{
					_innerStack.GrowMode = value;
				}
			}
		}

		public object SyncRoot
		{
			get 
			{
				return _innerStack.SyncRoot;
			}
		}

		public bool IsSynchronized
		{
			get 
			{ 
				return true; 
			}
		}


		#endregion

		#region ICollection<T> Members

		public void Add(T item)
		{
			lock (_syncRoot)
			{
				_innerStack.Add(item);
			}
		}

		public void Clear()
		{
			lock (_syncRoot)
			{
				_innerStack.Clear();
			}
		}

		public bool Contains(T item)
		{
			lock (_syncRoot)
			{
				return _innerStack.Contains(item);
			}
		}

		public void CopyTo(T[] array, int arrayIndex)
		{
			lock (_syncRoot)
			{
				_innerStack.CopyTo(array, arrayIndex);
			}
		}

		public int Count
		{
			get 
			{
				lock (_syncRoot)
				{
					return _innerStack.Count;
				}
			}
		}

		public bool IsReadOnly
		{
			get 
			{
				lock (_syncRoot)
				{
					return _innerStack.IsReadOnly;
				}
			}
		}

		public bool Remove(T item)
		{
			lock (_syncRoot)
			{
				return _innerStack.Remove(item);
			}
		}

		#endregion

		#region IEnumerable<T> Members

		public IEnumerator<T> GetEnumerator()
		{
			lock (_syncRoot)
			{
				return _innerStack.GetEnumerator();
			}
		}

		#endregion

		#region IEnumerable Members

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			lock (_syncRoot)
			{
				return _innerStack.GetEnumerator();
			}
		}

		#endregion

		#region ICloneable Members

		public object Clone()
		{
			lock (_syncRoot)
			{
				return _innerStack.Clone();
			}
		}

		#endregion


	}
}
